import uuid from 'uuid';
import jwt from 'jsonwebtoken';
import { User } from '../../models';
import config from '../../../config';

export const createJwtToken = async (accountId) => {
  const account = await User.findById(accountId);
  const ssoToken = uuid();
  account.set('ssoToken', ssoToken);
  account.set('lastLoginAt', new Date());
  await account.save();
  const accountTemp = account.toJSON();
  accountTemp.password = null;
  return jwt.sign(accountTemp, config.auth.jwt.secret, { expiresIn: '7d' });
};

export default {
  createJwtToken,
};
